/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-14 22:08:03
 * @LastEditTime: 2022-08-20 22:10:47
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




extern void H_NES_Mapper22_PRG_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper21_PRG_Read(Huint16 Address,void* v);
#define H_NES_Mapper22_PRG_Read H_NES_Mapper21_PRG_Read
extern Huint16 H_NES_Mapper21_PRG_Read16(Huint16 Address,void* v);
#define H_NES_Mapper22_PRG_Read16 H_NES_Mapper21_PRG_Read16
extern void H_NES_Mapper21_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v);
#define H_NES_Mapper22_CHR_Exception_Write H_NES_Mapper21_CHR_Exception_Write
extern void H_NES_Mapper21_CHR_Write(Hbyte Value,Huint16 Address,void* v);
#define H_NES_Mapper22_CHR_Write H_NES_Mapper21_CHR_Write
extern Hbyte H_NES_Mapper21_CHR_Read(Huint16 Address,void* v);
#define H_NES_Mapper22_CHR_Read H_NES_Mapper21_CHR_Read




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper21




void H_NES_Mapper22_MapperUpdate(void* v){
  H_NES_Console* Console;
  Hbyte* p;
  Huint32 ROM_Size;
  Huint32 Offset;
  Hbyte* t_bytep;
  int i;

  Console=v;
  
  p=Console->Cartridge.Mapper.PRG_ROM;
  ROM_Size=Console->Cartridge.Mapper.PRG_ROM_Size;

  Offset=((Huint32)Console->ThisMapper.Reg_800x)<<13;
  Offset=Offset%ROM_Size;
  t_bytep=&p[Offset];
  Console->ThisMapper.PRG_Banks.Base8000_8KB[0]=t_bytep;

  Offset=((Huint32)Console->ThisMapper.Reg_A00x)<<13;
  Offset=Offset%ROM_Size;
  t_bytep=&p[Offset];
  Console->ThisMapper.PRG_Banks.Base8000_8KB[1]=t_bytep;

  p=Console->Cartridge.Mapper.CHR_ROM;
  ROM_Size=Console->Cartridge.Mapper.CHR_ROM_Size;
  if(ROM_Size==0U){
    ROM_Size=0x2000U;
  }
  for(i=0;i<8;i++){
    Offset=((Huint32)Console->ThisMapper.Reg_B000_E003[2*i]&0x0FU)|(((Huint32)Console->ThisMapper.Reg_B000_E003[2*i+1])<<4);
    Offset<<=9;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[i]=t_bytep;
  }

}

void H_NES_Mapper22_PRG_Write_Ext(Hbyte Value,Huint16 Address,H_NES_Console* Console){
  Address=(Address&0xF000U)
    |(((Address&(Huint16)Console->ThisMapper.MskHBit)!=0U)?0x0002U:0U)
    |(((Address&(Huint16)Console->ThisMapper.MskLBit)!=0U)?0x0001U:0U);

  switch(Address&0xF000U){
    case 0x8000U:
      Console->ThisMapper.Reg_800x=Value;
      H_NES_Mapper22_MapperUpdate(Console);
      break;
    case 0xA000U:
      Console->ThisMapper.Reg_A00x=Value;
      H_NES_Mapper22_MapperUpdate(Console);
      break;
    case 0x9000U:
      if(Console->PPU.VRAM_ScreenType!=0x03U){
        if((Value&0x01U)==0x00U){
          //命名表水平排列
          Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
          Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0x400];
          Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
          Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
          Console->PPU.VRAM_ScreenType=0x02U;
        }else{
          //命名表垂直排列
          Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
          Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
          Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0x400];
          Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
          Console->PPU.VRAM_ScreenType=0x01U;
        }
      }
      break;
    case 0xF000U:
      break;
    default:
      //0xB000~0xE003
      Console->ThisMapper.Reg_B000_E003[(Address&0x0003U)+((Address-0xB000U)>>10)]=Value;
      H_NES_Mapper22_MapperUpdate(Console);
      break;
  }
}




static void GetProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultGetProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  p[0]=Console->ThisMapper.Reg_800x;
  p[1]=Console->ThisMapper.Reg_A00x;
  p[2]=Console->ThisMapper.Reg_9002;

  p[3]=Console->ThisMapper.Reg_F002;
  p[4]=Console->ThisMapper.Count;
  p[5]=Console->ThisMapper.CountReload;
  p[6]=((Hbyte*)&Console->ThisMapper.DivCount)[0];
  p[7]=((Hbyte*)&Console->ThisMapper.DivCount)[1];

  for(i=0;i<16;i++){
    p[8+i]=Console->ThisMapper.Reg_B000_E003[i];
  }

}

static void LoadProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultLoadProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  Console->ThisMapper.Reg_800x=p[0];
  Console->ThisMapper.Reg_A00x=p[1];
  Console->ThisMapper.Reg_9002=p[2];

  Console->ThisMapper.Reg_F002=p[3];
  Console->ThisMapper.Count=p[4];
  Console->ThisMapper.CountReload=p[5];
  ((Hbyte*)&Console->ThisMapper.DivCount)[0]=p[6];
  ((Hbyte*)&Console->ThisMapper.DivCount)[1]=p[7];

  for(i=0;i<16;i++){
    Console->ThisMapper.Reg_B000_E003[i]=p[8+i];
  }
  
  H_NES_Mapper22_MapperUpdate(Console);
}

int H_NES_Mapper22_Init(void* v){
  H_NES_Console* Console;
  int i;

  Console=v;


  if(Console->Cartridge.Mapper.PRG_RAM_Size<=0){//总是提供RAM
    Console->Cartridge.Mapper.PRG_RAM_Size=0x2000;
    Console->Cartridge.Mapper.PRG_RAM=Console->Memory.Malloc(0x2000);
    if(Console->Cartridge.Mapper.PRG_RAM==NULL){
      return vH_NES_Result_PRG_RAM_AllocFail;
    }
  }


  Console->ThisMapper.MskLBit=0x02U;
  Console->ThisMapper.MskHBit=0x01U;

  Console->ThisMapper.Reg_800x=0x00U;
  Console->ThisMapper.Reg_9002=0x01U;
  Console->ThisMapper.Reg_A00x=0x00U;

  Console->ThisMapper.Reg_F002=0x00U;
  Console->ThisMapper.Count=0x00U;
  Console->ThisMapper.CountReload=0x00U;
  Console->ThisMapper.DivCount=341;

  for(i=0;i<16;i++){
    Console->ThisMapper.Reg_B000_E003[i]=0x00U;
  }

  Console->ThisMapper.PRG_Banks.Base8000_8KB[3]=&Console->Cartridge.Mapper.PRG_ROM[Console->Cartridge.Mapper.PRG_ROM_Size-0x2000U];
  Console->ThisMapper.PRG_Banks.Base8000_8KB[2]=&Console->Cartridge.Mapper.PRG_ROM[Console->Cartridge.Mapper.PRG_ROM_Size-2U*0x2000U];

  H_NES_Mapper22_MapperUpdate(Console);


  Console->Cartridge.Mapper.DeInit=NULL;

  
  Console->Cartridge.PRG_Write=H_NES_Mapper22_PRG_Write;
  Console->Cartridge.PRG_Read=H_NES_Mapper22_PRG_Read;
  Console->Cartridge.PRG_Read16=H_NES_Mapper22_PRG_Read16;
  
  if(Console->Cartridge.Mapper.CHR_ROM_Size==0U){
    Console->Cartridge.CHR_Write=H_NES_Mapper22_CHR_Write;
  }else{
    Console->Cartridge.CHR_Write=H_NES_Mapper22_CHR_Exception_Write;
  }
  Console->Cartridge.CHR_Read=H_NES_Mapper22_CHR_Read;

  Console->Cartridge.Progress.Size=24+H_NES_Mapper_DefaultProgressSize(Console);
  Console->Cartridge.Progress.Get=GetProgress;
  Console->Cartridge.Progress.Load=LoadProgress;

  //Console->Cartridge.Option.CPUTick=NULL;
  
  

  return vH_NES_Result_OK;
}




#undef ThisMapper



